home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Sound / MoveAppBoundingLeaf.java.z / MoveAppBoundingLeaf.java
Encoding:
Java Source  |  2003-08-08  |  15.4 KB  |  383 lines

  1. /*
  2.  *    @(#)MoveAppBoundingLeaf.java 1.17 02/10/21 13:54:36
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. /*
  41.  * This Java3D program:
  42.  *    Creates an instance of the JavaSoundMixer AudioDevice, initializing it
  43.  *         and attaching it to the PhysicalEnvironment with a special version
  44.  *         of UniverseBuilder (UniverseBuildJS).
  45.  *    Creates one Point sound sources.
  46.  *    Creates two Soundscapes with their own aural attributes.  Each
  47.  *         of these Soundscapes defines its own bounding leaf region.
  48.  *    Creates and executes a custom behavior (AudioBehaviorMoveOne) that
  49.  *         starts sound playing and transforms the AuralAttribute positions
  50.  *         by modifying the TransformGroup that contains both soundscape
  51.  *         nodes.
  52.  * Usage: java MoveAppBoundingLeaf [ URLpath [ name ]]
  53.  *
  54.  * The first optional command line parameter is the URL path to directory
  55.  *    containing:
  56.  *       (1) the files that will be named on the command line, or
  57.  *       (2) the javaone/data/sounds directory that contains the default files
  58.  *    If not given the default path is:
  59.  *       file:/net/java3d/export/java3d/javaone/data/sounds
  60.  *    NOTE: This default path is only valid on Solaris Eng Menlo Park network.
  61.  *    A path must be supplied if sound files to be used are in a different
  62.  *    directory location.
  63.  *    NOTE: When running on Java3D Menlo Park Test WinTel platform path should
  64.  *    be set to
  65.  *       file:/java3d/export/java3d/javaone/data/sounds
  66.  *
  67.  * The second thru fourth optional command line parameters are sound file names
  68.  *    If not given the default file name is:
  69.  *       unicycle_close.au
  70.  */
  71.  
  72. import java.applet.Applet;
  73. import java.net.URL;
  74. import java.awt.*;
  75. import java.awt.event.*;
  76. import com.sun.j3d.utils.applet.MainFrame;
  77. import com.sun.j3d.utils.geometry.ColorCube;
  78. import com.sun.j3d.utils.geometry.Sphere;
  79. import com.sun.j3d.utils.universe.*;
  80. import java.io.File;
  81. import javax.media.j3d.*;
  82. import javax.vecmath.*;
  83.  
  84. public class MoveAppBoundingLeaf extends Applet {
  85.  
  86.     // File name of sound sample
  87.     private static  int filenamesGiven = 0;
  88.     private static URL[] url = new URL[1];
  89.     private static String[] filename = new String[1];
  90.     private static String path = null;
  91.     private static boolean filenamesSet = false;
  92.  
  93.     private SimpleUniverse u = null;
  94.  
  95.     public BranchGroup createSceneGraph() {
  96.     // Create the root of the subgraph
  97.     BranchGroup objRoot = new BranchGroup();
  98.  
  99.     // Create the transform group node and initialize it to the identity.
  100.     // Enable the TRANSFORM_WRITE capability so that our behavior code
  101.     // can modify it at runtime.  Add it to the root of the subgraph.
  102.     TransformGroup objTrans = new TransformGroup();
  103.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  104.     objRoot.addChild(objTrans);
  105.  
  106.     // Create a simple shape leaf node and add it into the scene graph.
  107.     objTrans.addChild(new ColorCube(0.7));
  108.  
  109.     // Create a new Behavior object that will perform the desired
  110.     // operation on the specified transform object and add it into the
  111.     // scene graph.
  112.     Transform3D yAxis = new Transform3D();
  113.         Alpha rotation = new Alpha(-1, Alpha.INCREASING_ENABLE,
  114.                                    0, 0,
  115.                                    20000, 0, 0,
  116.                                    0, 0, 0);
  117.         RotationInterpolator rotator =
  118.             new RotationInterpolator(rotation,
  119.                                      objTrans, yAxis,
  120.                                      0.0f, (float) Math.PI*2.0f);
  121.     BoundingSphere bounds =
  122.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  123.     rotator.setSchedulingBounds(bounds);
  124.     objTrans.addChild(rotator);
  125.         //
  126.         // Create a sound node and add it to the scene graph
  127.         //
  128.         PointSound sound = new PointSound();
  129.         sound.setCapability(PointSound.ALLOW_ENABLE_WRITE);
  130.         sound.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
  131.         sound.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
  132.         sound.setCapability(PointSound.ALLOW_SCHEDULING_BOUNDS_WRITE);
  133.         sound.setCapability(PointSound.ALLOW_CONT_PLAY_WRITE);
  134.         sound.setCapability(PointSound.ALLOW_RELEASE_WRITE);
  135.         sound.setCapability(PointSound.ALLOW_DURATION_READ);
  136.         sound.setCapability(PointSound.ALLOW_IS_PLAYING_READ);
  137.         sound.setCapability(PointSound.ALLOW_POSITION_WRITE);
  138.         sound.setCapability(PointSound.ALLOW_LOOP_WRITE);
  139.  
  140.     BoundingSphere soundBounds =
  141.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  142.         sound.setSchedulingBounds(soundBounds);
  143.     objTrans.addChild(sound);
  144.  
  145.         /*
  146.          * Spheres denoting aural attribute regions
  147.          */
  148.         TransformGroup objTransChild1 = new TransformGroup();
  149.         TransformGroup objTransChild2 = new TransformGroup();
  150.         objTransChild1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  151.         objTransChild2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  152.         Transform3D translate1 = new Transform3D();
  153.         Transform3D translate2 = new Transform3D();
  154.         Vector3f vector1 = new Vector3f( 2.0f, 0.0f, 0.0f);
  155.         Vector3f vector2 = new Vector3f(-2.0f, 0.0f, 0.0f);
  156.         translate1.setTranslation(vector1);
  157.         translate2.setTranslation(vector2);
  158.         objTransChild1.setTransform(translate1);
  159.         objTransChild2.setTransform(translate2);
  160.         Sphere sphere1 = new Sphere(0.42f);
  161.         Sphere sphere2 = new Sphere(0.38f);
  162.  
  163.         Appearance app = new Appearance();
  164.         app.setCapability( Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE );
  165.         sphere1.setAppearance( app );
  166.         sphere2.setAppearance( app );
  167.         objTransChild1.addChild(sphere1);
  168.         objTransChild2.addChild(sphere2);
  169.  
  170.         objTrans.addChild(objTransChild1);
  171.         objTrans.addChild(objTransChild2);
  172.         /**
  173.          * Define Soundscapes/AuralAttributes
  174.          */
  175.         // First Aural Attributes
  176.         Point2f[] distanceFilter = new Point2f[2];
  177.         distanceFilter[0] = new Point2f(6.0f, 6000f);
  178.         distanceFilter[1] = new Point2f(17.0f, 700f);
  179.         AuralAttributes attributes = new AuralAttributes(1.0f, 2.0f, 0.3f,
  180.                4.0f, 5, distanceFilter, 0.8f, 0.0f);
  181.         attributes.setReverbDelay(90.0f) ;
  182.         attributes.setReflectionCoefficient(0.999f) ;
  183.         attributes.setReverbOrder(9) ;
  184.  
  185.         // SoundScape
  186.     BoundingSphere bounds1 =
  187.         new BoundingSphere(new Point3d(2.0,0.0,0.0), 3.5);
  188.  
  189.         BoundingLeaf   sScapeBounds1 = new BoundingLeaf(bounds1);
  190.         Soundscape soundScape = new Soundscape(null, attributes);
  191.         soundScape.setApplicationBoundingLeaf(sScapeBounds1);
  192.         /** addChild of BoundingLeaf as well**/
  193.     objTrans.addChild(sScapeBounds1);
  194.  
  195.         AuralAttributes queryAttribs = new AuralAttributes();
  196.         if (queryAttribs == null)
  197.             System.out.println(" new AuralAttributes returned NULL");
  198.         // else
  199.         //  System.out.println(" new AuralAttributes returned " + queryAttribs);
  200.         queryAttribs = soundScape.getAuralAttributes();
  201.         if (queryAttribs == null)
  202.             System.out.println("getAuralAttributes returned NULL");
  203.         // else
  204.         //     System.out.println("AuralAttributes for Soundscape 1:");
  205.         float tmpFloat = queryAttribs.getAttributeGain();
  206.         // System.out.println("    Gain = " + tmpFloat);
  207.         tmpFloat = queryAttribs.getRolloff();
  208.         // System.out.println("    Rolloff = " + tmpFloat);
  209.         tmpFloat = queryAttribs.getReflectionCoefficient();
  210.         // System.out.println("    Reflection Coeff = " + tmpFloat);
  211.         tmpFloat = queryAttribs.getReverbDelay();
  212.         // System.out.println("    Delay = " + tmpFloat);
  213.         int tmpInt = queryAttribs.getReverbOrder();
  214.         // System.out.println("    Order = " + tmpInt);
  215.         int length = queryAttribs.getDistanceFilterLength();
  216.         // System.out.println("    Filter length = " + length);
  217.         Point2f[] tmpPoint = new Point2f[length];
  218.         tmpPoint = new Point2f[length];
  219.         for (int i=0; i< length; i++) 
  220.             tmpPoint[i] = new Point2f();
  221.         queryAttribs.getDistanceFilter(tmpPoint);
  222.         // for (int i=0; i< length; i++) 
  223.         //     System.out.println("    Distance Filter = (" + tmpPoint[i].x +
  224.         //         ", " + tmpPoint[i].y + ")" );
  225.         tmpFloat = queryAttribs.getFrequencyScaleFactor();
  226.         // System.out.println("    Freq scalefactor = " + tmpFloat);
  227.         tmpFloat = queryAttribs.getVelocityScaleFactor();
  228.         // System.out.println("    Velocity scalefactor= " + tmpFloat);
  229.     objTrans.addChild(soundScape);
  230.  
  231.         // System.out.println("SoundScape2**********************************");
  232.         Soundscape soundScape2 = new Soundscape();
  233.         distanceFilter = new Point2f[2];
  234.         distanceFilter[0] = new Point2f(2.0f, 20000.0f);
  235.         distanceFilter[1] = new Point2f(20.0f, 2000.0f);
  236.         AuralAttributes attributes2 = new AuralAttributes();
  237.         attributes2.setAttributeGain(1.2f);
  238.         attributes2.setRolloff(2.2f);
  239.         attributes2.setReverbDelay(1313.0f) ;
  240.         attributes2.setReflectionCoefficient(1.0f) ;
  241.         attributes2.setReverbOrder(15) ;
  242.         distanceFilter[0] = new Point2f(5.0f, 15000.0f);
  243.         distanceFilter[1] = new Point2f(15.0f, 500.0f);
  244.         attributes2.setDistanceFilter(distanceFilter);
  245.         attributes2.setFrequencyScaleFactor(0.8f);
  246.         attributes2.setVelocityScaleFactor(0.0f);
  247.     BoundingSphere bounds2 =
  248.         new BoundingSphere(new Point3d(-2.0,0.0,0.0), 0.38);
  249.         BoundingLeaf   sScapeBounds2 = new BoundingLeaf(bounds2);
  250.         soundScape2.setApplicationBoundingLeaf(sScapeBounds2);
  251.         // set BoundingLeaf as a child of transform node 
  252.     objTrans.addChild(sScapeBounds2);
  253.         soundScape2.setAuralAttributes(attributes2);
  254.  
  255.         queryAttribs = soundScape2.getAuralAttributes();
  256.         if (queryAttribs == null)
  257.             System.out.println(" new AuralAttributes returned NULL");
  258.         // else
  259.         //  System.out.println(" new AuralAttributes returned " + queryAttribs);
  260.         // System.out.println("AuralAttributes for Soundscape 2:");
  261.         tmpFloat = queryAttribs.getAttributeGain();
  262.         // System.out.println("    Gain = " + tmpFloat);
  263.         tmpFloat = queryAttribs.getRolloff();
  264.         // System.out.println("    Rolloff = " + tmpFloat);
  265.         tmpFloat = queryAttribs.getReflectionCoefficient();
  266.         // System.out.println("    Reflection Coeff = " + tmpFloat);
  267.         tmpFloat = queryAttribs.getReverbDelay();
  268.         // System.out.println("    Delay = " + tmpFloat);
  269.         tmpInt = queryAttribs.getReverbOrder();
  270.         // System.out.println("    Order = " + tmpInt);
  271.         length = queryAttribs.getDistanceFilterLength();
  272.         // System.out.println("    Filter length = " + length);
  273.         tmpPoint = new Point2f[length];
  274.         for (int i=0; i< length; i++) 
  275.             tmpPoint[i] = new Point2f();
  276.         queryAttribs.getDistanceFilter(tmpPoint);
  277.         // for (int i=0; i< length; i++) 
  278.         //     System.out.println("    Distance Filter = (" + tmpPoint[i].x +
  279.         //           ", " + tmpPoint[i].y + ")" );
  280.         tmpFloat = queryAttribs.getFrequencyScaleFactor();
  281.         // System.out.println("    Freq scalefactor = " + tmpFloat);
  282.         tmpFloat = queryAttribs.getVelocityScaleFactor();
  283.         // System.out.println("    Velocity scalefactor= " + tmpFloat);
  284.     objTrans.addChild(soundScape2);
  285.  
  286.         //
  287.     // Create a new Behavior object that will play the sound
  288.     //
  289.         AudioBehaviorMoveOne player = new AudioBehaviorMoveOne(sound, 
  290.                 filename[0]);
  291.     player.setSchedulingBounds(soundBounds);
  292.     objTrans.addChild(player);
  293.  
  294.     return objRoot;
  295.     }
  296.  
  297.     public MoveAppBoundingLeaf() {
  298.     }
  299.  
  300.     public void init() {
  301.         if (!filenamesSet) {
  302.             // path is null if started from applet
  303.         if (path == null) {
  304.             path = getCodeBase().toString();
  305.         }
  306.     
  307.             /*
  308.              * append given file name to given URL path
  309.              */
  310.             if (filenamesGiven > 0) {
  311.                 filename[0] = new String(path + "/" + filename[0]);
  312.             }
  313.             else {
  314.                 // fill in default file names if all three not given
  315.                 filename[0] = new String(path + "/techno_machine.au");
  316.             }
  317.             filenamesSet = true;
  318.         }
  319.  
  320.     setLayout(new BorderLayout());
  321.         GraphicsConfiguration config =
  322.            SimpleUniverse.getPreferredConfiguration();
  323.  
  324.         Canvas3D c = new Canvas3D(config);
  325.     add("Center", c);
  326.  
  327.         /*
  328.          * Change filenames into URLs
  329.          */
  330.         String substr = filename[0].substring(0,4);
  331.         try {
  332.             url[0] = new URL(filename[0]);
  333.         }
  334.         catch (Exception e) {
  335.                 return;
  336.         }
  337.         /*
  338.          * Create a simple scene and attach it to the virtual universe
  339.          */
  340.         u = new SimpleUniverse(c);
  341.         AudioDevice audioDev = u.getViewer().createAudioDevice();
  342.         BranchGroup scene = createSceneGraph();
  343.  
  344.         // This will move the ViewPlatform back a bit so the
  345.         // objects in the scene can be viewed.
  346.         u.getViewingPlatform().setNominalViewingTransform();
  347.  
  348.     u.addBranchGraph(scene);
  349.     }
  350.  
  351.     public void destroy() {
  352.     u.cleanup();
  353.     }
  354.  
  355.     //
  356.     // The following allows AuralAttributes to be run as an application
  357.     // as well as an applet
  358.     //
  359.     public static void main(String[] args) {
  360.         if (args.length > 0) {
  361.             if ( (args[0].startsWith("file"+File.pathSeparator)) ||
  362.                  (args[0].startsWith("http"+File.pathSeparator))   ) {
  363.                 path = args[0];
  364.             }
  365.             else {
  366.                 path = "file:" + args[0];
  367.             }
  368.         }
  369.         else {
  370.             path = "file:.";
  371.         } 
  372.         
  373.         if (args.length > 1) {
  374.             filename[0] = args[1];
  375.             if (filename[0] != null) {
  376.                 filenamesGiven++ ;
  377.             }
  378.         }
  379.  
  380.     new MainFrame(new MoveAppBoundingLeaf(), 256, 256);
  381.     }
  382. }
  383.